package com.ckerchina.admin.ckadmin.aop;

import org.apache.commons.lang3.ArrayUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//@Aspect
public class SpringAspectJLog {

	private Logger logger = LoggerFactory.getLogger("SpringAspectJLog");

	//@Around(value = "methodsToBeProfiled()")
	public Object profile(ProceedingJoinPoint pjp) throws Throwable {
		String name = Thread.currentThread().getName();
		String staticPart = pjp.getSignature().toString();
		int line = pjp.getSourceLocation().getLine();
		Object[] args = pjp.getArgs();
		String params = ArrayUtils.toString(args, "");
		Long costTime = null;
		long startTime = System.currentTimeMillis();
		Object result = null;
		try {
			logger.info(name + " 开始请求{}:{}\t{}", staticPart, line, params);
			result = pjp.proceed();
			return result;
		} finally {
			long stopTime = System.currentTimeMillis();
			costTime = stopTime - startTime;
			logger.info(name + " 结束请求{},耗时:{}毫秒", staticPart, costTime);
			logger.debug(name + " 结束请求{},返回:{}", staticPart, result);
		}
	}

	//@Pointcut(value = "execution(public * com.zhuzi.controller..*Controller.*(..))")
	public void methodsToBeProfiled() {
	}

}
